using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using NBear.Common;

namespace NBear.Test.UnitTests
{
    namespace _Entity
    {
        public class User2
        {
            public static PropertyItem ID = new PropertyItem("ID");
            public static PropertyItem Name = new PropertyItem("UserName");
        }

        public class SmartEntity
        {
            public static PropertyItem ID = new PropertyItem("ID");
            public static PropertyItem Name = new PropertyItem("Name");
        }
    }

    public interface SmartEntity : IEntity
    {
        [PrimaryKey]
        int ID { get; }
        [ColumnName("EntityName")]
        string Name { get; }
    }

    /// <summary>
    /// Summary description for TestPropertyQuery
    /// </summary>
    [TestClass]
    public class TestEntityQuery
    {
        public TestEntityQuery()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestBasicWhereClip()
        {
            Assert.AreEqual((_Entity.User2.ID == _Entity.User2.Name).ToString(), "[|ID|] = [|UserName|]");
            Assert.AreEqual((_Entity.User2.ID != _Entity.User2.Name).ToString(), "[|ID|] <> [|UserName|]");
            //Assert.AreEqual((_Entity.User.ID == 10).ToString(), "[ID] = @ID");
            Console.WriteLine((_Entity.User2.ID == 10).ToString());
            Assert.AreEqual((int)((_Entity.User2.ID == 10).ParamValues[0]), 10);
            //Assert.AreEqual(("teddy" == _Entity.User.Name).ToString(), "@UserName = [UserName]");
            Console.WriteLine(("teddy" == _Entity.User2.Name).ToString());
            //Assert.AreEqual(_Entity.User.Name.Like("%teddy%").ToString(), "[UserName] LIKE @UserName");
            Console.WriteLine(_Entity.User2.Name.Like("%teddy%").ToString());
            Assert.AreEqual(_Entity.User2.Name.Like("%teddy%").ParamValues[0], "%teddy%");
            //Assert.AreEqual((_Entity.User.ID == _Entity.User.Name & _Entity.User.Name.Like("%teddy%")).ToString(), "([ID] = [UserName]) AND ([UserName] LIKE @UserName)");
            Console.WriteLine((_Entity.User2.ID == _Entity.User2.Name & _Entity.User2.Name.Like("%teddy%")).ToString());
        }

        [TestMethod]
        public void TestBasicOrderByClip()
        {
            Assert.AreEqual((_Entity.User2.ID.Asc & _Entity.User2.Name.Desc).ToString(), "[|ID|], [|UserName|] DESC");
        }

        [TestMethod]
        public void TestSmartEntityQuery()
        {
            Console.WriteLine((_Entity.SmartEntity.ID == 10).ToString());
            Console.WriteLine(PropertyItem.ParseExpressionByMetaData((_Entity.SmartEntity.ID == 10).ToString(), new PropertyToColumnMapHandler(Entity<SmartEntity>.GetPropertyMappedColumn), "[", "]", "@"));
            Console.WriteLine((_Entity.SmartEntity.ID == 10 & _Entity.SmartEntity.Name > "teddy").ToString());
            Console.WriteLine(PropertyItem.ParseExpressionByMetaData((_Entity.SmartEntity.ID == 10 & _Entity.SmartEntity.Name > "teddy").ToString(), new PropertyToColumnMapHandler(Entity<SmartEntity>.GetPropertyMappedColumn), "\"", "\"", "?"));
            Console.WriteLine((_Entity.SmartEntity.ID.Asc & _Entity.SmartEntity.Name.Desc).ToString());
            Console.WriteLine(PropertyItem.ParseExpressionByMetaData((_Entity.SmartEntity.ID.Asc & _Entity.SmartEntity.Name.Desc).ToString(), new PropertyToColumnMapHandler(Entity<SmartEntity>.GetPropertyMappedColumn), "[", "]", "@"));
        }
    }
}
